package cn.wjdiankong.levideo.list.adapter;

import java.util.ArrayList;
import java.util.List;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.wjdiankong.levideo.R;
import cn.wjdiankong.levideo.data.LevideoData;
import cn.wjdiankong.levideo.utils.ImageLoaderUtils;
import cn.wjdiankong.levideo.utils.Utils;

public class ToutiaoVideoListAdapter extends BaseAdapter{
	
	protected List<LevideoData> dataList = new ArrayList<LevideoData>();
	protected Context mContext;
	
	protected ImageLoader imageLoader = null;
	protected DisplayImageOptions userIconOptions, imgOptions, musicOptions;
	
	public ToutiaoVideoListAdapter(Context ctx){
		mContext = ctx;
		imageLoader = ImageLoaderUtils.getInstance(ctx).getImageLoader();
	}

	@Override
	public int getCount() {
		return dataList.size();
	}
	
	public void setDataList(List<LevideoData> dataList){
		if(dataList != null && dataList.size() > 0){
			this.dataList.addAll(dataList);
			notifyDataSetChanged();
		}
	}
	
	public void removeData(LevideoData data){
		if(dataList != null && dataList.size() > 0){
			dataList.remove(data);
			notifyDataSetChanged();
		}
	}
	
	public void removeData(int position){
		if(dataList != null && dataList.size() > 0 && position < dataList.size()){
			dataList.remove(position);
			notifyDataSetChanged();
		}
	}
	
	public List<LevideoData> getDataList(){
		return dataList;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		View view = convertView;
		if (view == null || view.getTag() == null) {
			view = LayoutInflater.from(mContext).inflate(R.layout.localvideo_toutiao_adapter_item_layout, parent, false);
			viewHolder = new ViewHolder();
			viewHolder.textV = (TextView) view.findViewById(R.id.content_txt);
			viewHolder.imgV = (ImageView) view.findViewById(R.id.content_img);
			viewHolder.timeTxt = (TextView)view.findViewById(R.id.time_txt);
			viewHolder.playIcon = view.findViewById(R.id.play_icon);
			viewHolder.durationTimeTxt = (TextView) view.findViewById(R.id.duration_txt);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}
		viewHolder.textV.setText(dataList.get(position).title);
		if(!TextUtils.isEmpty(dataList.get(position).formatTimeStr)){
			viewHolder.timeTxt.setText("保存于: "+dataList.get(position).formatTimeStr);
			viewHolder.timeTxt.setVisibility(View.VISIBLE);
		}else{
			viewHolder.timeTxt.setVisibility(View.GONE);
		}
		viewHolder.durationTimeTxt.setText(Utils.formatDuration(dataList.get(position).videoDuration));
		ImageLoader.getInstance().displayImage("file://" + dataList.get(position).coverImgUrl, viewHolder.imgV);
		return view;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
	
	public static class ViewHolder{
		public TextView textV;//标题
		public ImageView imgV;//封面图
		public TextView timeTxt;//时间
		public View playIcon;//播放按钮
		public TextView durationTimeTxt;//时长
	}
	
	public String formatContent(String str){
		try{
			return str.split("】")[0].split("【")[1];
		}catch(Exception e){
			return str;
		}
	}

}
